package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsWithdrawRecordBo;
import com.kxmall.huishou.domain.vo.HsWithdrawRecordVo;
import com.kxmall.huishou.domain.HsWithdrawRecord;
import com.kxmall.huishou.mapper.HsWithdrawRecordMapper;
import com.kxmall.huishou.service.IHsWithdrawRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户提现记录Service业务层处理
 *
 * @author lyrics
 * @date 2023-12-14
 */
@RequiredArgsConstructor
@Service
public class HsWithdrawRecordServiceImpl implements IHsWithdrawRecordService {

    private final HsWithdrawRecordMapper baseMapper;

    /**
     * 查询用户提现记录
     */
    @Override
    public HsWithdrawRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户提现记录列表
     */
    @Override
    public TableDataInfo<HsWithdrawRecordVo> queryPageList(HsWithdrawRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWithdrawRecord> lqw = buildQueryWrapper(bo);
        Page<HsWithdrawRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户提现记录列表
     */
    @Override
    public List<HsWithdrawRecordVo> queryList(HsWithdrawRecordBo bo) {
        LambdaQueryWrapper<HsWithdrawRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWithdrawRecord> buildQueryWrapper(HsWithdrawRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWithdrawRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HsWithdrawRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getAccountType() != null, HsWithdrawRecord::getAccountType, bo.getAccountType());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), HsWithdrawRecord::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getRealName()), HsWithdrawRecord::getRealName, bo.getRealName());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), HsWithdrawRecord::getBankName, bo.getBankName());
        lqw.eq(bo.getStatus() != null, HsWithdrawRecord::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUsername()), HsWithdrawRecord::getUsername, bo.getUsername());
        return lqw;
    }

    /**
     * 新增用户提现记录
     */
    @Override
    public Boolean insertByBo(HsWithdrawRecordBo bo) {
        HsWithdrawRecord add = BeanUtil.toBean(bo, HsWithdrawRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户提现记录
     */
    @Override
    public Boolean updateByBo(HsWithdrawRecordBo bo) {
        HsWithdrawRecord update = BeanUtil.toBean(bo, HsWithdrawRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWithdrawRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户提现记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
